package com.dq.mail.exception;

import com.dq.mail.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @ClassName: GlobalExceptionHandler
 * @Description: 全局捕获异常
 * @Author: app-backend-public_小强 (QQ:2680825683)
 * @Since: 2023/3/3 15:04
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {


    @ExceptionHandler(value = CommonException.class)
    public R doCommonExceptionHandle(CommonException e) {
        //1. 可以log打印  可以统一返回前端数据
        String errMessage = e.getMessage();
        log.error("捕获异常信息:{}", e.getErrorMsg());
        return R.error().message(e.getErrorMsg());
    }


    //捕获不可预知异常  例如MYSQL connection timeOut
    @ExceptionHandler(value = Exception.class)
    // @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public R doExceptionHandler(Exception e) { //此方法捕获  XueChengNormalException异常  并报出500状态码
        //1. 可以log打印  可以统一返回前端数据
        String errMessage = e.getMessage();
        log.error("捕获异常信息:{}", e.getMessage());
        return R.error().message(CommonErrorEnum.UNKOWN_ERROR.getErrMessage());
    }
}
